package com.heima.article.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojo.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @projectName: heima-leadnews
 * @package: com.heima.article.listener
 * @className: ArticleIsDownListener
 * @author: 躺着认真玩
 * @description: 文章上下架-kafka
 * @date: 2025/5/26 8:58
 * @version: 1.0
 */
@Component
@Slf4j
public class ArticleIsDownListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @KafkaListener(topics = "wm.news.up.or.down.topic")
    public void handArticle(String message){
        // 校验参数
        if (!StrUtil.isBlank(message)) {
            Map map = JSONUtil.toBean(message, Map.class);
            //    根据文章id对配置表进行修改
            apArticleConfigMapper.update(null,
                    new LambdaUpdateWrapper<ApArticleConfig>().eq(ApArticleConfig::getArticleId,map.get("articleId"))
                            .set(ApArticleConfig::getIsDown,map.get("enable")));
        }
        return;
    }
}
